import { reactive, ref, toRefs } from "vue";
type paginationParams = {
  pageNum: number;
  pageSize: number;
  id: number
};
export default function useTable<
  T extends { list: any[]; total: number },
  P extends paginationParams = paginationParams>(fn: (params: P) => RES<T>, searchParams?: Object) {
  // 固定的部分，分页的参数

  const params = reactive<paginationParams>({
    pageNum: 1,
    pageSize: 10,
    id: 0,
  })
  const list = ref<T['list']>([]);
  const total = ref(0);
  const fetch = () => {
    fn({ ...(params as P), ...searchParams }).then((res) => {
      if (res.code === 200) {
        list.value = res.data.list || res.data;
        total.value = res.data.total;
      }
    })
  }
  fetch()
  const pageChangeHandle = () => {
    fetch()
  }
  const pageSizeChangeHandle = () => {
    fetch()
  }
  // 搜索
  const searchParamsFn = () => {
    fetch()
  }
  const deleteParamsFn = () => {
    fetch()
  }

  const removeDataFn = () => {
    fetch();
  };

  const searchHandle = () => {
    fetch();
  };


  return {
    list,
    ...toRefs(params),
    total,
    pageChangeHandle,
    pageSizeChangeHandle,
    searchParamsFn,
    deleteParamsFn,
    removeDataFn,
    searchHandle,
  }
}



